﻿// -----------------------------------------------------------------------
// <copyright file="DmLoaiVatTuDataAccess.cs" company="">
// TODO: Copyright by dangppsoft@gmail.com.
// </copyright>
// -----------------------------------------------------------------------
namespace ManagerClinic.Core.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ManagerClinic.Core.Common;
    using System.Data.Common;
    using System.Data;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using Microsoft.Practices.EnterpriseLibrary.Common;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using ManagerClinic.Ultilities.Ultility;

    public class DmLoaiVatTuDataAccess
    {
        public bool InsertDmLoaiVatTu(DmLoaiVatTuEntity obj)
        {
            try
            {
                string sql = "spDmLoaiVatTu_Insert";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaLoaiVatTu", DbType.String, obj.MaLoaiVatTu);
                db.AddInParameter(cmd, "TenLoaiVatTu", DbType.String, obj.TenLoaiVatTu);
                db.AddInParameter(cmd, "MoTa", DbType.String, obj.MoTa);
                db.AddInParameter(cmd, "TrangThaiSuDung", DbType.Boolean, obj.TrangThaiSuDung);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("InsertDmLoaiVatTu", ex.Message); throw ex; }
        }

        public bool EditDmLoaiVatTu(DmLoaiVatTuEntity obj)
        {
            try
            {
                string sql = "spDmLoaiVatTu_Update";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaLoaiVatTu", DbType.String, obj.MaLoaiVatTu);
                db.AddInParameter(cmd, "TenLoaiVatTu", DbType.String, obj.TenLoaiVatTu);
                db.AddInParameter(cmd, "MoTa", DbType.String, obj.MoTa);
                db.AddInParameter(cmd, "TrangThaiSuDung", DbType.Boolean, obj.TrangThaiSuDung);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("UpdateDmLoaiVatTu", ex.Message); throw ex; }
        }

        public bool DeleteDmLoaiVatTu(string maLoaiVatTu)
        {
            try
            {
                string sql = "spDmLoaiVatTu_DeleteByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaLoaiVatTu", DbType.String, maLoaiVatTu);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("DeleteDmLoaiVatTu", ex.Message); throw ex; }
        }

        public DmLoaiVatTuEntity GetDmLoaiVatTu(string maLoaiVatTu)
        {
            try
            {
                DmLoaiVatTuEntity obj = null;
                string sql = "spDmLoaiVatTu_SelectByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaLoaiVatTu", DbType.String, maLoaiVatTu);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new DmLoaiVatTuEntity();
                        obj.MaLoaiVatTu = reader["MaLoaiVatTu"].ToString();
                        obj.TenLoaiVatTu = reader["TenLoaiVatTu"].ToString();
                        obj.MoTa = reader["MoTa"].ToString();
                        obj.TrangThaiSuDung = String.IsNullOrEmpty(reader["TrangThaiSuDung"].ToString()) == true ? false : (bool)reader["TrangThaiSuDung"];
                    }
                }
                return obj;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetDmLoaiVatTu", ex.Message); throw ex; }
        }

        public List<DmLoaiVatTuEntity> GetDmLoaiVatTuList()
        {
            try
            {
                List<DmLoaiVatTuEntity> list = new List<DmLoaiVatTuEntity>();
                DmLoaiVatTuEntity obj = null;
                string sql = "spDmLoaiVatTu_SelectAll";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new DmLoaiVatTuEntity();
                        obj.MaLoaiVatTu = reader["MaLoaiVatTu"].ToString();
                        obj.TenLoaiVatTu = reader["TenLoaiVatTu"].ToString();
                        obj.MoTa = reader["MoTa"].ToString();
                        obj.TrangThaiSuDung = String.IsNullOrEmpty(reader["TrangThaiSuDung"].ToString()) == true ? false : (bool)reader["TrangThaiSuDung"];
                        list.Add(obj);
                    }
                }
                return list;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetDmLoaiVatTuList", ex.Message); throw ex; }
        }

    }
}